package me.xADudex.RandomLocation;

import java.io.*;
import java.util.zip.*;

class FileTools
{
    static String read(final File f) {
        String result = null;
        DataInputStream in = null;
        try {
            final byte[] buffer = new byte[(int)f.length()];
            in = new DataInputStream(new FileInputStream(f));
            in.readFully(buffer);
            result = new String(buffer);
        }
        catch (IOException e) {
            throw new RuntimeException("IO problem in fileToString", e);
        }
        finally {
            try {
                in.close();
            }
            catch (IOException ex) {}
        }
        try {
            in.close();
        }
        catch (IOException ex2) {}
        return result;
    }
    
    static void write(final File f, final String string) {
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            final BufferedWriter out = new BufferedWriter(new FileWriter(f));
            out.write(string);
            out.close();
        }
        catch (IOException e) {
            System.out.println("Error: Could not write file!\nCheck that the file exists!");
        }
    }
    
    static void zipFiles(final File zipFile, final File... files) {
        try {
            final byte[] buffer = new byte[1024];
            final FileOutputStream fout = new FileOutputStream(zipFile);
            final ZipOutputStream zout = new ZipOutputStream(fout);
            for (int i = 0; i < files.length; ++i) {
                if (files[i] != null) {
                    if (files[i].exists()) {
                        final FileInputStream fin = new FileInputStream(files[i].getAbsolutePath());
                        zout.putNextEntry(new ZipEntry(files[i].getName()));
                        int length;
                        while ((length = fin.read(buffer)) > 0) {
                            zout.write(buffer, 0, length);
                        }
                        zout.closeEntry();
                        fin.close();
                    }
                }
            }
            zout.close();
        }
        catch (IOException ioe) {
            System.out.println("IOException :" + ioe);
        }
    }
}
